/*
 * 日志消息类
 *
 * 综合日志消息需要的各项要素：
 * 1. 产生时间
 * 2. 日志等级
 * 3. 源文件名
 * 4. 代码行号
 * 5. 函数名称
 * 6. 线程名称
 * 7. 日志器名
 * 8. 消息载荷
 *
 **/

#pragma once

#include <thread>
#include "util.hpp"
#include "level.hpp"

namespace easylog
{
    struct message
    {
        time_t          time;
        level::lv_enum  levo;
        std::string     file;
        size_t          line;
        std::string     func;
        std::thread::id tid;
        std::string     lname;
        std::string     payload;
        bool            flush;

        message() = default;

        message(level::lv_enum lv, const char* fl, size_t ln, const char* fc, const char* name, std::string pload, bool flh = false)
            : time(util::time::now())
            , levo(lv)
            , file(fl)
            , line(ln)
            , func(fc)
            , tid(std::this_thread::get_id())
            , lname(name)
            , payload(pload)
            , flush(flh)
        {}

        // This constructor is provided for the filename pattern parser
        // or for async logger push a flush task.
        message(int tp)
        {
            if (tp == _4parse)
            {
                time = util::time::now();
                file = __FILE__;
                line = __LINE__;
                func = __FUNCTION__;
                tid  = std::this_thread::get_id();
            }
            else if (tp == _4flush)
            {
                type = _4flush;
            }
            else
            {
                throw except("unknown message type");
            }
        }

        enum
        {
            unknown = 0,
            _4parse,
            _4flush,
        } type;

        bool is4flush() { return type == _4flush; }
        bool is4parse() { return type == _4parse; }

        message& set4flush() { type = _4flush; return *this; }
        message& set4parse() { type = _4parse; return *this; }
    };
}
